package cn.xinfei.xdecision.manager.controller.component.rulebase;

import cn.xinfei.xdecision.common.model.common.PageResult;
import cn.xinfei.xdecision.common.model.common.ResponseEntityBuilder;
import cn.xinfei.xdecision.common.model.common.ResponseEntityDto;
import cn.xinfei.xdecision.common.model.common.requestParam.AssemblyRuleCodeReq;
import cn.xinfei.xdecision.common.model.component.guiderule.vo.RuleVersionRespVo;
import cn.xinfei.xdecision.common.model.component.scriptrule.RuleScriptVersion;
import cn.xinfei.xdecision.common.service.component.scriptrule.RuleScriptVersionService;
import cn.xinfei.xdecision.common.utils.constant.OperateNameConst;
import cn.xinfei.xdecision.common.utils.constant.OperateTypeEnum;
import cn.xinfei.xdecision.manager.web.aop.ArchivesLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * (RuleScriptVersion)表控制层
 */
@Api(tags = "组件中心-规则库")
@RestController
@RequestMapping("/v3/ruleScriptVersion")
@ResponseBody
public class RuleScriptVersionController {
    /**
     * 服务对象
     */
    @Resource
    private RuleScriptVersionService ruleScriptVersionService;

    /**
     * 查询指定版本下的内容
     * @param versionId
     * @return
     */
    @PostMapping("/getRuleScriptVersion/{versionId}")
    public ResponseEntityDto getRuleVersionInfo(@PathVariable Long versionId) {
        RuleScriptVersion version = ruleScriptVersionService.queryById(versionId);
        return ResponseEntityBuilder.buildNormalResponse(version);
    }

    /**
     * 新增一个版本
     * @param version
     * @return
     */
    @PostMapping("/addRuleScriptVersion")
	@ArchivesLog(type = OperateTypeEnum.CREATE,name = OperateNameConst.ComponentOpt.SAVE_VERSION)
    public ResponseEntityDto addRuleVersion(@RequestBody RuleScriptVersion version){
        return ResponseEntityBuilder.buildNormalResponse(ruleScriptVersionService.checkAndAddVersion(version));
    }

    /**
     * 修改版本
     * @param version
     * @return
     */
    @PostMapping("/updateRuleScriptVersion")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.UPDATE_VERSION)
    public ResponseEntityDto updateRuleVersion(@RequestBody RuleScriptVersion version){
        boolean b = ruleScriptVersionService.updateVersion(version);
        List<RuleScriptVersion> ruleVersionList = ruleScriptVersionService.queryVersionListByRuleId(version.getRuleId());
        return ResponseEntityBuilder.buildNormalResponse(ruleVersionList);
    }


    @ApiOperation(value = "锁定版本")
    @PostMapping(value = "/lock/{versionId}")
	@ArchivesLog(type = OperateTypeEnum.UPDATE,name = OperateNameConst.ComponentOpt.LOCK_VERSION)
    public ResponseEntityDto<String> lock(@PathVariable Long versionId) {
        ruleScriptVersionService.lock(versionId);
        return ResponseEntityBuilder.buildNormalResponse("锁定版本成功!");
    }


	@RequestMapping(value = "queryVersionInfos", method = RequestMethod.POST)
	@ApiOperation(value = "查询脚本规则版本列表", notes = "查询脚本规则版本列表")
	public ResponseEntityDto<PageResult<List<RuleVersionRespVo>, RuleScriptVersion>> getRuleInfo(@Valid @RequestBody AssemblyRuleCodeReq req) {
		return ruleScriptVersionService.listByRuleCode(req);
	}
}
